package com.ving.test.mybatis.statement;

import com.ving.test.mybatis.binding.MapperMethod;
import com.ving.test.mybatis.resultset.ResultSetHandler;
import com.ving.test.mybatis.session.Configuration;
import com.ving.test.mybatis.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * Created by liuweijie on 2019-11-14.
 */
public class StatementHandler {
    private Configuration configuration;
    private ResultSetHandler resultSetHandler;


    public StatementHandler(Configuration configuration){
        this.configuration = configuration;
        this.resultSetHandler = new ResultSetHandler();

    }

    public <T>T query(MapperMethod mapperMethod,Object parameter) throws Exception {
        Connection connection = DBUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(String.format(mapperMethod.getSql(),Integer.parseInt((String.valueOf(parameter)))));
//        PreparedStatement preparedStatement = DBUtil.getConnection().prepareStatement(String.format(mapperMethod.getSql()));
//        preparedStatement.setString(1,String.valueOf(parameter));
//        preparedStatement.execute();
        preparedStatement.execute();

        return resultSetHandler.handle(preparedStatement,mapperMethod);


    }
}
